//
//  SEGActivityCollectUploadView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/9.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityCollectUploadView.h"

static const CGFloat kActPicPadding = 15.0;
static const CGFloat kActPicSize = 75.0;
static const NSUInteger kActAddButtonTag = 1000;

@interface SEGActivityCollectUploadView ()<UITextFieldDelegate,UITextViewDelegate>
/**
 描述提示
 */
@property (nonatomic, strong)UILabel *describeLabel;

/**
 图片父视图
 */
@property (nonatomic, strong)UIView *picContentView;

/**
 上传图片按钮
 */
@property (nonatomic, strong)UIButton *addButton;


/**
 一行的最大图片数
 */
@property (nonatomic, assign) NSUInteger maxPicNumberInRow;
@end

@implementation SEGActivityCollectUploadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;

    // 描述输入框
    self.describeView = [[UITextView alloc] init];
    self.describeView.font = kFont_13;
    self.describeView.textColor = kColor_1;
    //    self.describeView.returnKeyType = UIReturnKeyDone;
    self.describeView.tintColor = kColor_T;
    [self addSubview:self.describeView];
    [self.describeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(12);
        make.right.equalTo(self).with.offset(-12);
        make.top.equalTo(self).with.offset(6);
        make.height.mas_equalTo(73.0);
    }];
    self.describeView.delegate = self;
    
    //提示
    UILabel *describeLabel = [[UILabel alloc] init];
    describeLabel.text = @"您想说点什么呢？（140字）";
    describeLabel.font = kFont_13;
    describeLabel.textColor = kColor_C;
    [self addSubview:describeLabel];
    [describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(15);
        make.right.equalTo(self).with.offset(-15);
        make.top.equalTo(self).with.offset(14);
    }];
    self.describeLabel = describeLabel;
    
    // 图片视图
    self.picContentView = [[UIView alloc] init];
    //    self.picContentView.backgroundColor = kColor_S1;
    [self addSubview:self.picContentView];
    [self.picContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(15);
        make.right.equalTo(self).with.offset(-15);
        make.top.equalTo(self).with.offset(99).priorityHigh();
        make.bottom.equalTo(self).with.offset(-25);
    }];
    self.maxPicNumberInRow = (LYScreenWidth() - 15) / (kActPicSize + kActPicPadding);
    
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.addButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_addpic")] forState:UIControlStateNormal];
    [self.addButton addTarget:self action:@selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.picContentView addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.picContentView);
        make.top.equalTo(self.picContentView).offset(15);
        make.width.mas_equalTo(kActPicSize);
        make.height.mas_equalTo(kActPicSize);
        make.bottom.equalTo(self).offset(-20);
    }];
    self.addButton.tag = kActAddButtonTag;

    self.isImageNeedLoad = YES;
}

/**
 * 设置图片数组和URL数组
 * @author Wendy
 * @param picArray 保存上传图片数组
 * @param urlArray 保存上传图片url的数组
 */
- (void)setPicArray:(NSMutableArray *)picArray urlArray:(NSMutableArray *)urlArray
{
    self.picArray = picArray;
    self.urlArray = urlArray;
    [self updatePicContentView];
}

/**
 * 刷新图片界面
 * @author Wendy
 */
- (void)updatePicContentView
{
    //移除子视图
    [self.picContentView.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) {
        //添加按钮不移除
        if(obj.tag != kActAddButtonTag) {
            [obj removeFromSuperview];
        }
    }];
    
    [self.picArray enumerateObjectsUsingBlock:^(UIImage *obj, NSUInteger idx, BOOL *stop) {
        
        CGFloat leftPadding = (idx % self.maxPicNumberInRow) * (kActPicPadding + kActPicSize);
        CGFloat topPadding = (idx / self.maxPicNumberInRow) * (kActPicPadding + kActPicSize);
        
        UIImageView* picImageView = [[UIImageView alloc] init];
        [picImageView setImage:obj];
        [picImageView.layer setMasksToBounds:YES];
        picImageView.layer.cornerRadius = 3;
        picImageView.contentMode = UIViewContentModeScaleAspectFill;
        picImageView.userInteractionEnabled = YES;
        [self.picContentView addSubview:picImageView];
        [picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.picContentView).with.offset(leftPadding);
            make.top.equalTo(self.picContentView).with.offset(topPadding);
            make.width.height.mas_equalTo(kActPicSize);
        }];
        //添加点击手势
        picImageView.tag = 10 + idx;
        [picImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]];
        
        // 显示删除按钮
        UIImage* closeImage = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_delete")];
        UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeBtn setImage:closeImage forState:UIControlStateNormal];
        [closeBtn addTarget:self action:@selector(removeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        closeBtn.tag = 100 + idx;
        [self.picContentView addSubview:closeBtn];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(picImageView.mas_right).with.offset(-13);
            make.bottom.equalTo(picImageView.mas_top).with.offset(13);
            make.width.height.mas_equalTo(26.0);//图片范围17.0 不好点 加大点击区域
        }];
        
    }];
    
    [self setAddButtonWithArray:self.picArray];
}

- (void)setAddButtonWithArray:(NSArray *)array
{
    if ([array count] >= SEGActivityPhotoMax) {
        self.addButton.hidden = YES;
    }else {
        self.addButton.hidden = NO;
        CGFloat leftPadding = (array.count % self.maxPicNumberInRow) * (kActPicPadding + kActPicSize);
        CGFloat topPadding = (array.count / self.maxPicNumberInRow) * (kActPicPadding + kActPicSize);
        [self.addButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.picContentView).with.offset(leftPadding);
            make.top.equalTo(self.picContentView).with.offset(topPadding);
        }];
    }
}


- (void)tapImage:(UITapGestureRecognizer *)tap
{
    NSUInteger index = tap.view.tag - 10;
    if (self.delegate && [self.delegate respondsToSelector:@selector(tapPicAtIndex:)]) {
        [self.delegate tapPicAtIndex:index];
    }
}

/**
 * 添加按钮触发方法
 * @author Wendy
 */
- (void)addButtonPressed
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addPicAction)]) {
        [self.delegate addPicAction];
    }
}

/**
 * 删除图片按钮触发方法
 * @author Wendy
 */
- (void)removeBtnAction:(UIButton*)sender
{
    NSUInteger index = sender.tag - 100;
    if (self.delegate && [self.delegate respondsToSelector:@selector(removePicAtIndex:)]) {
        [self.delegate removePicAtIndex:index];
    }
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView*)textView
{
    if ([[textView text] length] > 0) {
        [self.describeLabel setHidden:YES];
    }
    else {
        [self.describeLabel setHidden:NO];
    }
}

//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
//{
//    if ([text isEqualToString:@"\n"]) {
//        [textView resignFirstResponder];
//        return NO;
//    }
//    return YES;
//}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end

